﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyThuVien.DataAccess;
using QuanLyThuVien.Model;



namespace QuanLyThuVien.View
{
    public partial class FormSach : Form
    {
        SachAccess sachAccess = new SachAccess();
        DataTable dssach = new DataTable();
        public FormSach()
        {
            InitializeComponent();
        }
        private void FormSach_Load(object sender, EventArgs e)
        {

            dssach = sachAccess.laytoanbosach();
            dataGridView1.DataSource = dssach;


        }
     
        private void btnSua_Click(object sender, EventArgs e)
        {
         Sach s = new Sach(txtMaSach.Text,txtTenSach.Text,txtTheLoai.Text,txtTinhTrang.Text,int.Parse(txtSoLuong.Text),txtNhaXB.Text,txtTacGia.Text,int.Parse(txtNamXB.Text));
            if (sachAccess.suasach(s)==0 ) { MessageBox.Show("Mã sach không tồn tại !"); }
            else
            {
                MessageBox.Show("Sửa thành công !");
            }
           FormSach_Load(sender,e);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaSach.Text=dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTenSach.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtTheLoai.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtTinhTrang.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtSoLuong.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtNhaXB.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            txtTacGia.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            txtNamXB.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();

        }

        private void txt_TimSach_TextChanged(object sender, EventArgs e)
        {
             DataView dataView = new DataView();
            if (cbb_TimSach.SelectedItem==cbb_TimSach.Items[0])
            {
                dataView =sachAccess.timkiemMasach(txt_TimSach.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = sachAccess.timkiemTensach(txt_TimSach.Text);
                dataGridView1.DataSource = dataView;
            }
        }

        private void cbb_TimSach_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
             Sach s = new Sach(txtMaSach.Text, txtTenSach.Text, txtTheLoai.Text, txtTinhTrang.Text, int.Parse(txtSoLuong.Text), txtNhaXB.Text, txtTacGia.Text, int.Parse(txtNamXB.Text));
            if (sachAccess.xoasach(s) == 0) { MessageBox.Show("Mã sách không tồn tại !"); }
            else
            {
                MessageBox.Show("Xóa thành công !");
            }
            FormSach_Load(sender, e);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Sach s = new Sach(txtMaSach.Text, txtTenSach.Text, txtTheLoai.Text, txtTinhTrang.Text, int.Parse(txtSoLuong.Text), txtNhaXB.Text, txtTacGia.Text, int.Parse(txtNamXB.Text));
            if (sachAccess.themsach(s) == 0) { MessageBox.Show("Mã sách đã tồn tại !"); }
            else
            {
                MessageBox.Show("Thêm thành công !");
            }
            FormSach_Load(sender, e);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            
            if (checkBox1.Checked == true)
            {
                txtMaSach.ReadOnly = false;
                //cbbLopHoc.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                txtMaSach.ReadOnly = true;
                //cbbLopHoc.Enabled = false;
                btnThem.Enabled = false;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

    }
}

       
    


